/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package formula.node;

import java.util.Hashtable;

/**
 *
 * @author CJ
 */
public class ValueNode extends Node{

    protected double value;

    protected String name;

    public ValueNode(double value, String name, int depth){
        super(depth);
        this.value = value;
        this.name = name;
    }

    public double getValue(){
        return this.value;
    }

    public void setValue(ValueNode node){
        this.value = node.value;
        this.name = node.name;
    }

    public String toString(){
        String result = "";
        if(name==null){
            result += this.value;
        } else {
            result += name;
        }
        return result;
    }

    public double eval(Hashtable<String, Double> varables){
        double returnValue;
        try{
            return varables.get(name);
        }catch(Exception e){
            //Not found return our value
            return this.value;
        }
    }

    public ValueNode clone(){
        return new ValueNode(this.value, this.name, this.depth);
    }

    public boolean reducable(){
        if(name != null)
            return false;
        return true;
    }

}
